var app = getApp()
Page({
  data: {
    windowWidth: 0, // 可使用窗口宽度
    windowHeight: 0, // 可使用窗口高度
    ratio: 0, // 根据尺寸动态计算 1px换算成多少rpx
    list:[],
    id:'',
    study_member:'0',
    study_percent:'0',
    hiddenName: 'hide',
    cur: '0',//改变当前索引
    index: 0,//当前的索引
    wximage: '',
    xiaochengxu: 0
  },
  swiperChange(e) {
    var that =this
    let current = e.detail.current;
    that.setData({
      index: e.detail.current
    })
    if (current%4==0){
      that.getList()
    }
  },
  getList: function () {
    var that = this
    
    wx.request({
      url: 'https://xiaochengxu.fincoder.com//index.php/Home/Talk/index3',
      data: {
        id: that.data.id,
        myId: app.globalData.myId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status === 1) {
          console.log(res)
          var tem = that.data.list
          for (var i = 0; i < res.data.data.length; i++) {
  
            tem.push(res.data.data[i]);
          }
          that.setData({
            list:tem,
            study_percent: res.data.study_percent,
            study_member: res.data.study_member,
          })
          wx.hideLoading()
        } else {
        }
      }, fail: function () {
        wx.hideLoading()
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  toTxtBook: function () {
    wx.navigateTo({
      url: '../txtbook/txtbook',
    })
  },
  onShoww: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(index)
    const ctx = wx.createCanvasContext('myCanvas');
    var txtsharebg1 = "../../resource/txtsharebg1.png";
    var txtsharebg2 = "../../resource/txtsharebg2.png";
    var xxx = '../../resource/xxx.png';
    let canvasWidthPx = 690 / this.data.ratio;
    if(index%2==0){
      ctx.drawImage(txtsharebg1, 15, 15, canvasWidthPx, this.data.windowHeight - 76 * this.data.ratio);
    }else{
      ctx.drawImage(txtsharebg2, 15, 15, canvasWidthPx, this.data.windowHeight - 76 * this.data.ratio);
    }
   
    //填充背景  
    var text = that.data.list[index].content;
    var percent = that.data.study_percent;
    var member = that.data.study_member;
    var chr = text.split("");//这个方法是将一个字符串分割成字符串数组
    var temp = "";
    var row = [];
    ctx.setFontSize(15)
    ctx.setFillStyle("#fff")
    for (var a = 0; a < chr.length; a++) {
      if (ctx.measureText(temp).width < 280) {
        temp += chr[a];
      }
      else {
        a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
        row.push(temp);
        temp = "";
      }
    }
    row.push(temp);
    for (var b = 0; b < row.length; b++) {
      ctx.fillText(row[b], 40, 60 + b * 30, 320);
    }
    //绘制介绍
    ctx.fillStyle = "rgba(0,0,0,0.5)";
    ctx.fillRect(30, this.data.windowHeight - 106 * this.data.ratio, canvasWidthPx - 110, 50);
    ctx.setFontSize(12);
    ctx.setFillStyle('#fff');
    ctx.fillText(member + '人正在学习，你的商业逻辑', 40, this.data.windowHeight - 96 * this.data.ratio);
    //绘制介绍
    ctx.setFontSize(12);
    ctx.setFillStyle('#fff');
    ctx.fillText('思维能力已经超越' + percent + '的企业家', 40, this.data.windowHeight - 86 * this.data.ratio);
    ctx.drawImage(xxx, canvasWidthPx - 60, this.data.windowHeight - 106 * this.data.ratio, 50, 50);
    ctx.draw();
    this.setData({
      hiddenName: 'show'
    })
  },
  onhide: function () {
    var that = this;
    this.setData({
      hiddenName: 'hide'
    })
  },

  savetup: function () {
    var that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 750,
      height: 1136,
      destWidth: 1500,
      destHeight: 2272,
      canvasId: 'myCanvas',
      success: function (res) {
        //调取小程序当中获取图片
        console.log(res.tempFilePath);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log(res.tempFilePath);
            wx.showModal({
              title: '成功保存图片',
              content: '已成功为您保存图片到手机相册，请自行前往查看',
              showCancel: false,
              confirmText: '知道了',
              confirmColor: '#A4E65D',
              success: function (res) {
                if (res.confirm) {
                  that.onhide()
                }
              }
            })
          },
           fail: function (res) {
             console.log("打开设置窗口");
             if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
               
               wx.openSetting({
                 success(settingdata) {
                   console.log(settingdata)
                   if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                     console.log("获取权限成功，再次点击图片保存到相册")
                   } else {
                     console.log("获取权限失败")
                   }
                 }
               })
             }
          }
        })
        
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  onLoad: function (query) {
    var that = this
    that.getList()
    let scene = decodeURIComponent(query.scene);
    console.log(scene)
    if (scene > 0) {
      console.log(scene)
      that.setData({
        id: scene
      })
    }
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/banben.php',

      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res.data.xiaochengxu)
        console.log("====")
        that.setData({
          xiaochengxu: res.data.xiaochengxu,

        })

      }, fail: function () {
        wx.hideLoading()
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  
  /**
* 用户点击右上角分享
*/
  onShareAppMessage: function () {
    return {
      title: "A计划",
      path: 'pages/index/page/languagetxt/languagetxt?id=' + that.data.id,
    }
  },
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({
      url: '/pages/index/index',
    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })
    // 获取系统信息
    wx.getSystemInfo({
      success: res => {
        console.log(res);
        this.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight,
        });
        this.setData({
          ratio: 750 / this.data.windowWidth
        });
      }
    });
  },

})